using System;
using System.Collections.Generic;
using System.Text;
using SharpMud.Text.Matching;

namespace SharpMud.Text.Dynamic.Processing
{
    /// <summary>
    /// Pairs an IStringMatcherMatch with the nugget type it is
    /// </summary>
    internal class MatchInfo
    {
        private MatchInfo() {}
        public MatchInfo(System.Type nuggetType, IStringMatcherMatch match)
        {
            if (match == null)
                throw new ArgumentNullException("match");
            if (nuggetType == null)
                throw new ArgumentNullException("nuggetType");
            this.NuggetType = nuggetType;
            this.Match = match;
        }
        public MatchInfo(IStringMatcherMatch match, System.Type nuggetType)
        {
            if (match == null)
                throw new ArgumentNullException("match");
            if (nuggetType == null)
                throw new ArgumentNullException("nuggetType");
            this.NuggetType = nuggetType;
            this.Match = match;
        }

        public static MatchInfo Make(IStringMatcherMatch match, System.Type nuggetType)
        {
            if (match == null)
                return null;
            if (nuggetType == null)
                throw new ArgumentNullException("nuggetType");

            MatchInfo mi = new MatchInfo();
            mi.Match = match;
            mi.NuggetType = nuggetType;
            return mi;
        }
        public static MatchInfo Make(System.Type nuggetType, IStringMatcherMatch match)
        {
            if (match == null)
                return null;
            if (nuggetType == null)
                throw new ArgumentNullException("nuggetType");

            MatchInfo mi = new MatchInfo();
            mi.Match = match;
            mi.NuggetType = nuggetType;
            return mi;
        }

        public System.Type NuggetType;
        public IStringMatcherMatch Match;

        public static int CompareMatchInfosAscending(MatchInfo left, MatchInfo right)
        {
            int index_diff = left.Match.Index - right.Match.Index;

            if (index_diff != 0)
                return index_diff;
            else
                return left.Match.Length - right.Match.Length;
        }

        public static int CompareMatchesAscending(IStringMatcherMatch left, IStringMatcherMatch right)
        {
            int index_diff = right.Index - left.Index;

            if (index_diff != 0)
                return index_diff;
            else
                return right.Length - left.Length;
        }

        public static bool IsUselessMatchInfo(MatchInfo matchInfo)
        {
            if (matchInfo.Match == null)
                return true;
            else if (matchInfo.Match.Length == 0)
                return true;
            else
                return false;
        }
    }
}
